var skyboxEffectVertexShaderStr = "\
attribute vec3 inPosition;\
attribute vec2 inTexCoord0;\
uniform mat4 viewProjectMat;\
uniform vec3 viewPosition;\
varying vec2 outTexcoord;\
void main(void)\
{\
  gl_Position = viewProjectMat*vec4(inPosition+viewPosition,1.0);\
  outTexcoord = inTexCoord0;\
}\
";
var skyboxEffectFragmentShaderStr = "\
varying vec2 outTexcoord;\
uniform sampler2D skyMap;\
uniform vec4 skyColor;\
void main(void)\
{\
  gl_FragColor = vec4(mix( texture2D(skyMap,outTexcoord).rgb, skyColor.rgb,skyColor.a ),1.0);\
}\
";
//
//gl_FragColor = textureCube(skyMap,uv  );\
function gewSkyboxEffect(game,texture,size)
{
  // General properties
  this.game = game;
  this.gl = game.gl;
  var gl = game.gl;
  this.name = "SkyboxEffect";
  this.scene3d = null;//Current scene3d that using this effect 
  this.materialList = {};
  this.currentPass = null;
   
  // Shader properties
  createGLSLShader(gl,this,skyboxEffectVertexShaderStr,skyboxEffectFragmentShaderStr);
  
  this.boxHalfSize = size;
  var vertices = new WebGLFloatArray( [-size,0,-size, // 0  
                                       -size,0,-size, // 1       
                                        size,0,-size, // 2
                                        size,0,-size, // 3      
                                        size,0,size,  // 4
                                        size,0,size,  // 5     
                                        -size,0,size, // 6
                                        -size,0,size, // 7
                                        -size,size,-size,//8   
                                        size,size,-size,   //9
                                        size,size,size,   //10
                                        -size,size,size]);//11
                                        
  var texcoords = new WebGLFloatArray( [0,0.75, // 0  
                                        0.25,1, // 1       
                                        0.75,1, // 2
                                        1,0.75, // 3      
                                        1,0.25,  // 4
                                        0.75,0,  // 5     
                                        0.25,0, // 6
                                        0,0.25, // 7
                                        0.25,0.75,//8   
                                        0.75,0.75,//9
                                        0.75,0.25,   //10
                                        0.25,0.25,
                                        
                                        0,0.75, // 0  
                                        0.25,1, // 1       
                                        0.75,1, // 2
                                        1,0.75, // 3      
                                        1,0.25,  // 4
                                        0.75,0,  // 5     
                                        0.25,0, // 6
                                        0,0.25, // 7
                                        0.25,0.75,//8   
                                        0.75,0.75,//9
                                        0.75,0.25,   //10
                                        0.25,0.25]);//11  
  var indices = new WebGLUnsignedShortArray([0,8,7,8,7,11,
                                             1,2,8,2,8,9,
                                             3,4,9,4,9,10,
                                             5,6,10,6,10,11,
                                             8,9,10,8,10,11]
                                            );                                    
  var ver_buff = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, ver_buff);
  gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
  var tex_buff = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, tex_buff);
  gl.bufferData(gl.ARRAY_BUFFER, texcoords, gl.STATIC_DRAW);
  var ind_buff = gl.createBuffer();
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ind_buff);
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
  this.glBuffers = {};
  this.glBuffers.position = ver_buff;
  this.glBuffers.texcoord = tex_buff;
  this.glBuffers.indices = ind_buff;
  
  this.texture = texture;
  this.color = new gewVec4(0,0,0,0);
}
gewSkyboxEffect.prototype.destructor = function()
{
  this.scene3d.removeEffect(this);
},
gewSkyboxEffect.prototype.render = function(scene3d) 
{
  var gl = this.gl; 
  this.scene3d = scene3d;
  var texture = this.texture;
  if(texture&& texture.isReady)
  {
    // push GL state
    // set GL state
    gl.useProgram(this.program);
    gl.enableVertexAttribArray(this.inPosition);
    gl.enableVertexAttribArray(this.inTexCoord0);
    this.currentPass = 0;
    gl.uniform1i(this.skyMap, 0);
    gl.activeTexture(gl.TEXTURE0);
    gl.bindTexture(gl.TEXTURE_2D, texture.glName);
    
    var color = this.color;
    gl.uniform4f(this.skyColor,color.x,color.y,color.z,color.w);
    var viewprojMat = scene3d.viewProjectMat;
    gl.uniformMatrix4fv(this.viewProjectMat, false, viewprojMat.data);
    var viewPos = scene3d.viewPosition;
    gl.uniform3f(this.viewPosition, viewPos.x, 0.0, viewPos.z);
    
    gl.bindBuffer(gl.ARRAY_BUFFER, this.glBuffers.position);
    gl.vertexAttribPointer(this.inPosition, 3, gl.FLOAT, false, 0, 0);
    gl.bindBuffer(gl.ARRAY_BUFFER, this.glBuffers.texcoord);
    gl.vertexAttribPointer(this.inTexCoord0, 2, gl.FLOAT, false, 0, 0);
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.glBuffers.indices);
    gl.drawElements(gl.TRIANGLES,30,gl.UNSIGNED_SHORT,0);
    
    gl.currentPass = null;
    // pop GL state  
    gl.disableVertexAttribArray(this.inPosition);
    gl.disableVertexAttribArray(this.inTexCoord0);
  }
}
